/**
 入驻小区
 **/
(function (vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            allocationOrderManageInfo: {
                allocationOrders: [],
                shopHouses: [],
                total: 0,
                records: 1,
                moreCondition: false,
                aiId: '',
                curOrder: {},
                states: [],
                conditions: {
                    shId: '',
                    orderId: '',
                    srcShId: '',
                    targetShId: '',
                    staffName: '',
                    state: '',
                    shopId: ''
                }
            }
        },
        _initMethod: function () {
            $that._listShopHouses();
            $that._listAssetInventorys(DEFAULT_PAGE, DEFAULT_ROWS);
            vc.getDict('allocation_order', "state", function (_data) {
                $that.allocationOrderManageInfo.states = _data;
            });
        },
        _initEvent: function () {
            vc.on('allocationOrderManage', 'listAssetInventory', function (_param) {
                $that._listAssetInventorys(DEFAULT_PAGE, DEFAULT_ROWS);
            });
            vc.on('allocationOrderManage', 'notifyAuditInfo', function (_param) {
                $that._auditAllocationOrderState(_param);
            })
            vc.on('pagination', 'page_event', function (_currentPage) {
                $that._listAssetInventorys(_currentPage, DEFAULT_ROWS);
            });
        },
        methods: {
            _listAssetInventorys: function (_page, _rows) {
                $that.allocationOrderManageInfo.conditions.page = _page;
                $that.allocationOrderManageInfo.conditions.row = _rows;
                $that.allocationOrderManageInfo.conditions.shopId = vc.getCurrentCommunity().shopId;
                
                let param = {
                    params: $that.allocationOrderManageInfo.conditions
                };
                param.params.orderId = param.params.orderId.trim();
                //发送get请求
                vc.http.apiGet('/allocationOrder/queryAllocationOrder',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.allocationOrderManageInfo.total = _json.total;
                        $that.allocationOrderManageInfo.records = _json.records;
                        $that.allocationOrderManageInfo.allocationOrders = _json.data;
                        vc.emit('pagination', 'init', {
                                total: $that.allocationOrderManageInfo.records,
                                dataCount: $that.allocationOrderManageInfo.total,
                                currentPage: _page
                            }
                        );
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            //调拨申请
            _openAddAssetInventoryModal: function () {
                vc.jumpToPage('/#/pages/goods/allocationOrderIn')
            },
            //修改
            _openEditAssetInventoryModel: function (_allocationOrder) {
                // vc.jumpToPage('/#/pages/goods/allocationOrderEdit?aiId=' + _allocationOrder.aiId);
                vc.emit('allocationOrderEdit', 'openEditAllocationOrderModal', _allocationOrder);
            },
            //入库
            _doAllocationOrderInModel: function (_inoutOrder) {
                vc.jumpToPage('/#/pages/goods/doAllocationOrderIn?orderId=' + _inoutOrder.orderId + "&staffName=" + _inoutOrder.staffName
                    + "&stateName=" + _inoutOrder.stateName + "&createTime=" + _inoutOrder.createTime + "&stateRemark=" + _inoutOrder.stateRemark
                    + "&shopName=" + _inoutOrder.shopName + "&srcShName=" + _inoutOrder.srcShName + "&targetShopName=" + _inoutOrder.targetShopName
                    + "&targetShName=" + _inoutOrder.targetShName + "&shopId=" + _inoutOrder.shopId + "&specId=" + _inoutOrder.specId + "&productId=" + _inoutOrder.productId
                    + "&productCount=" + _inoutOrder.productCount + "&state=" + _inoutOrder.state + "&targetShId=" + _inoutOrder.targetShId + "&prodName=" + _inoutOrder.prodName
                    + "&srcShId=" + _inoutOrder.srcShId + "&targetShopId=" + _inoutOrder.targetShopId + "&staffId=" + _inoutOrder.staffId);
            },
            //审核
            _openInStockAssetInventoryModel: function (_order) {
                $that.allocationOrderManageInfo.curOrder = _order;
                vc.emit('allocationOrderAudit', 'openAllocationOrderAuditModal', {});
            },
            //删除
            _openDeleteAssetInventoryModel: function (_allocationOrder) {
                vc.emit('deleteAllocationOrder', 'openDeleteAllocationOrderModal', _allocationOrder);
            },
            //查询
            _queryAssetInventoryMethod: function () {
                $that._listAssetInventorys(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            //重置
            _resetAssetInventoryMethod: function () {
                $that.allocationOrderManageInfo.conditions.srcShId = "";
                $that.allocationOrderManageInfo.conditions.targetShId = "";
                $that.allocationOrderManageInfo.conditions.state = "";
                $that.allocationOrderManageInfo.conditions.orderId = "";
                $that._listAssetInventorys(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            _getInvLoss: function (_allocationOrder) {
                let _invLoss = parseInt(_allocationOrder.quantity) - parseInt(_allocationOrder.invQuantity);
                if (_invLoss > 0) {
                    return _invLoss;
                }
                return 0;
            },
            _getInvProfit: function (_allocationOrder) {
                let _invProfit = parseInt(_allocationOrder.invQuantity) - parseInt(_allocationOrder.quantity);
                if (_invProfit > 0) {
                    return _invProfit;
                }
                return 0;
            },
            _getInvLossMoney: function (_allocationOrder) {
                let _invLoss = parseInt(_allocationOrder.quantityMoney) - parseInt(_allocationOrder.invQuantityMoney);
                if (_invLoss > 0) {
                    return _invLoss;
                }
                return 0;
            },
            _getInvProfitMoney: function (_allocationOrder) {
                let _invProfit = parseInt(_allocationOrder.invQuantityMoney) - parseInt(_allocationOrder.quantityMoney);
                if (_invProfit > 0) {
                    return _invProfit;
                }
                return 0;
            },
            _moreCondition: function () {
                if ($that.allocationOrderManageInfo.moreCondition) {
                    $that.allocationOrderManageInfo.moreCondition = false;
                } else {
                    $that.allocationOrderManageInfo.moreCondition = true;
                }
            },
            _listShopHouses: function (_page, _rows) {
                var param = {
                    params: {
                        page: 1,
                        row: 100,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet('/shopHouse.queryShopHouse',
                    param,
                    function (json, res) {
                        var _shopHouseManageInfo = JSON.parse(json);
                        $that.allocationOrderManageInfo.shopHouses = _shopHouseManageInfo.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _auditAllocationOrderState: function (_auditInfo) {
                let _order = $that.allocationOrderManageInfo.curOrder;
                _order.state = _auditInfo.state;
                _order.stateRemark = _auditInfo.remark;
                _order.flag = "1";
                vc.http.apiPost(
                    '/allocationOrder/auditAllocationOrder',
                    JSON.stringify(_order),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        if (res.status == 200) {
                            //关闭model
                            vc.toast("审核成功");
                            $that._listAssetInventorys(DEFAULT_PAGE, DEFAULT_ROWS);
                            return;
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    }
                );
            }
        }
    });
})(window.vc);
